package liudan.archetype;

import javax.servlet.Servlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Vector;

/**
 * Description:
 * Email: liudan@ucfgroup.com
 * Title
 * Copyright: Copyright (c) 2017
 * User: liudan
 * Created on 2017/3/10 22:58
 */
public class PropertiesTest {

    private static String APP_FILES = "jdbc.properties";
    private static Properties APP_PROPER = null;

    public static void main(String[] args) {
        System.out.println(PropertiesTest.getProps("aa"));
    }
    /***
     * 获取KEY
     *
     * @param key
     * @return
     */
    public static String getProps(String key) {
        if (APP_PROPER == null) {
            synchronized (PropertiesTest.class) {
                if (APP_PROPER == null) {
                    APP_PROPER = reloadProperties(APP_FILES);
                }
            }
        }
        return APP_PROPER.getProperty(key);
    }

    /***
     * 读取Properties
     *
     * @param file
     * @return
     */
    public static Properties reloadProperties(String file) {
        InputStream in = PropertiesTest.class.getClassLoader().getResourceAsStream(file);
        Properties pro = new Properties();
        try {
            pro.load(in);
        } catch (IOException e) {
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
        return pro;
    }
}
